﻿@model Wicresoft.UnifyShow.Business.SFUser_XModel
@{
    ViewBag.Title = "创建用户";
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
    List<string> sites = (List<string>)ViewData["Sites"];
}
<script src="@Url.Content("~/Scripts/message_validate.js")" type="text/javascript"></script>
<div class="index_lr">
    @if (ViewBag.Post)
    {
        <input type="hidden" value="@ViewBag.Status" id="CreateStatus" />
        <input type="hidden" value="@ViewBag.ResultStr" id="ResultStr" />
        <script type="text/javascript">

            $(document).ready(function () {
                var status = parseInt($("#CreateStatus").val());
                var resultStr = $("#ResultStr").val();
                var error = "连接错误，无法成功添加此用户，请重新点击添加，或联系管理员。"
                if (resultStr != "") error += ("(" + resultStr + ")");
                switch (status) {
                    case 0:
                        $("#resetForm").click();
                        alert("添加成功。");
                        break;
                    case 1:
                        alert("集中展现" + error);
                        break;
                    case 2:
                        alert("自服务" + error);
                        break;
                    case 3:
                        alert("ITSM" + error);
                        break;
                    case 4:
                        alert("认证服务" + error);
                        break;
                    default:
                        alert("服务器" + error);
                }

            });
        </script> 
    }
    <ul>
        <li class="index_lr2_M">
            <div class="index_lrA_title ui-icons">
                <h3>
                    创建用户</h3>
            </div>
            @using (Html.BeginForm())
            {
                <div class="index_pt_bg">
                    <table id="inputs">
                        <tbody>
                            <tr>
                                <td width="350">
                                    <label>
                                        <b>姓名<font class="err">*</font>：</b></label>@Html.EditorFor(o => o.DisplayName)
                                    <span id="spDisplayName" class="errMsg"></span>
                                </td>
                                <td width="380">
                                    <label>
                                        <b>公司<font class="err">*</font>：</b></label><select name="company" id="company" onchange="companyselect(this)">
                                            @if (string.IsNullOrWhiteSpace(Model.Company) || Model.Company == "福建省烟草专卖局")
                                            {
                                                <option value="请选择">---请选择---</option>
                                                foreach (var item in sites)
                                                {
                                                <option value="@item">@item</option>
                                                }
                                            }
                                            else
                                            {
                                                <option value=@Model.Company selected="selected">@Model.Company</option>
                                            }
                                        </select>
                                </td>
                            </tr>
                            <tr>
                                <td width="350">
                                    <label>
                                        <b>登录名<font class="err">*</font>：</b></label>@Html.EditorFor(o => o.Name)
                                    <span id="spName" class="errMsg"></span>
                                </td>
                                <td width="470">
                                    <label>
                                        <b>部门<font class="err">*</font>： </b>
                                    </label>
                                    <select name="department" id="Department" style="width: 210px;">
                                    </select>（部门应为ITSM存在部门）
                                </td>
                            </tr>
                            <tr>
                                <td width="350">
                                    <label>
                                        <b>密码<font class="err">*</font>：</b></label><input type="password" id="Password" name="Password" value="@Model.Password" style="width:210px;"/>
                                    <span id="spPassword" class="errMsg"></span>
                                </td>
                                <td width="380">
                                    <label>
                                        <b>确认<font class="err">*</font>：</b></label><input type="password" id="ConfirmPassword" name="ConfirmPassword" value="@Model.ConfirmPassword" style="width:210px;" /><br />
                                    <span id="spConfirmPassword" class="errMsg"></span>
                                </td>
                            </tr>
                            <tr>
                                <td width="350">
                                    <label>
                                        <b>邮箱<font class="err">*</font>：</b></label>@Html.EditorFor(model => model.Email)
                                    <span id="spEmail" class="errMsg"></span>
                                </td>
                                <td width="380">
                                    <label>
                                        <b>联系方式<font class="err">*</font>：</b></label>@Html.EditorFor(model => model.Mobile)<br />
                                    <span id="spMobile" class="errMsg"></span>
                                </td>
                            </tr>
                            <tr>
                                <td width="350">
                                    <label>
                                        <b>职务<font class="err">*</font>：</b></label>@Html.EditorFor(model => model.Position)
                                    <span id="spPosition" class="errMsg"></span>
                                </td>
                                <td width="380">
                                    <label>
                                        <b>员工编码<font class="err">*</font>：</b></label>@Html.EditorFor(model => model.PeopleNo)
                                    <span id="spPosition" class="errMsg"></span>
                                </td>
                            </tr>
                            <tr>
                                <td width="380">
                                    <label>
                                        <b>统一登录门户：</b></label>@Html.EditorFor(model => model.IsRelevance)
                                </td>
                            </tr>
                        </tbody>
                    </table>
                    <p>
                        <input type="submit" value="添加" id="addSubmit" />
                        <input type="button" value="重置" id="resetForm" />
                    </p>
                </div>
            }
        </li>
    </ul>
</div>
@section ScriptAndCss{
    <style>
        label
        {
            float: left;
            width: 100px;
            text-align: right;
        }
        .fieldset1 input
        {
            float: left;
        }
        input[type="password"]
        {
            width: 210px;
        }
        input[type="text"]
        {
            width: 210px;
        }
        .errMsg
        {
            color: red;
            margin-left: 100px;
            font-size: 11px;
            font-style: italic;
            display: none;
        }
    </style>
    <script type="text/javascript">
        window.onload = function () {
            $("#company").change();
        }

        function clearForm() {
            $("#company", "#inputs")[0].selectedIndex = 0;
            $("#company").change();
            $("input", "#inputs").each(function () {
                $(this).val("");
            });
        }

        var error11 = false;

        function companyselect(obj) {
            $('#Department').empty();
            $('#Department').append($("<option value='请选择'>---请选择---</option>"));
            var rnd = (new Date()).getMilliseconds() + "" + parseInt(Math.random() * 10000);
            var company = $("#company").val();
            var url = "/AdminPermission/GetDepartment?company=" + encodeURI(company) + "&nocatch=" + rnd;
            $.getJSON(url, function (data) {
                $.each(data, function (i, item) {
                    $('#Department').append($("<option></option>").val(item).html(item));
                });
            });

        }

        $(document).ready(function () {

            $("form").submit(function () {
                //var error = "";
                //if ($("#DisplayName").val() == "") error += "用户名不能为空。";
                //if ($("#Name").val() == "") error += "登录名不能为空。";
                //if ($("#Mobile").val() == "") error += "手机不能为空。";
                //if ($("#Password").val() == "") error += "密码不能为空。";
                //if ($("#Email").val() == "") error += "邮箱不能为空。";
                //if ($("#Department").val() == "") error += "部门不能为空。";
                //if ($.trim($("#Position").val()) == "") error += "职务不能为空。";
                //if ($("#ConfirmPassword").val() != $("#Password").val()) error += "两次密码输入不一致。";
                var company = $("#company").val();
                var department = $('#Department').val();
                if (company == "请选择") {
                    alert("请选择用户所属公司！");
                    return false;
                }
                if (department == "请选择") {
                    alert("请选择用户所属部门！");
                    return false;
                }
                if (error11) {
                    alert("登录名已经存在，请重新输入。");
                    return false;
                }
                else if (!docheck()) {
                    alert("信息输入有误，请核对后再提交。");
                    return false;
                }
                else {
                    return true;
                }
            });

            $("#Name").focusout(function () {
                if ($(this).val() != "") {
                    $.post('@Url.Action("Exist", "AdminPermission")', "name=" + $(this).val(), function (data) {
                        if (data == true) {
                            error11 = true;
                            alert("登录名已经存在，请重新输入。");
                            $(this).focus();
                        } else {
                            error11 = false;
                        }
                    });
                }
            });

            $("#resetForm").click(function () {
                clearForm();
            });

            customMessageVaildate();
        });

    </script>
}
